package com.share.hystrix;

import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixObservableCommand;
import lombok.Data;
import rx.Observable;
import rx.functions.Func1;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author:caifan
 * date:2019/7/15
 */
public class ObservableCommandNumbersToWords extends HystrixObservableCommand<ObservableCommandNumbersToWords.NumberWord> {
    private final List<Integer> numbers;

    static Map<Integer, String> dict = new HashMap<>(11);
    static
    {
        dict.put(0, "zero");
        dict.put(1, "one");
        dict.put(2, "two");
        dict.put(3, "three");
        dict.put(4, "four");
        dict.put(5, "five");
        dict.put(6, "six");
        dict.put(7, "seven");
        dict.put(8, "eight");
        dict.put(9, "nine");
        dict.put(10, "ten");
    }

    public ObservableCommandNumbersToWords(final List<Integer> numbers)
    {
        super(HystrixCommandGroupKey.Factory.asKey(ObservableCommandNumbersToWords.class.getName()));
        this.numbers = numbers;
    }

    @Override
    protected Observable<NumberWord> construct() {
        return Observable.from(numbers).map(new Func1<Integer, NumberWord>()
        {
            @Override
            public NumberWord call(final Integer number)
            {
                return new NumberWord(number, dict.get(number));
            }

        });
    }

    @Data
    static class NumberWord {
        private final Integer number;
        private final String word;
    }
}
